<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );
$query = "SELECT count(*) " .
		"FROM ruler " .
		"WHERE user_id = " . $_SESSION[ 'user_id' ];
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array( $result, MYSQL_ASSOC );
global $language;
if( $row[ 'count(*)' ] > 0 ) {
	header( "Location: ../overview/overview.php" );
}
else {
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<meta http-equiv="Content-Style-Type" content="text/css" />
		<title>Ages of Strife: <?php transprint( 'Choose ruler name' ); ?></title>
		<link rel="stylesheet" href="agesofstrife.css"></link>
	</head>
	<body>
		<div id="root_container">
			<div align="center" id="header-image">
				<a href="../index.php?language=<?php print( $language ); ?>">
					<img class="header-dummy-image" src="../resources/dummy.gif" alt="Ages of Strife" border="0" />
				</a>
			</div>
<?php
include( "ads.php" );
?>
				<div id="container">
					<div id="main-content">
						<h2><?php transprint( 'Choose ruler name' ); ?></h2>
<?php
	if( isset( $_SESSION[ 'create_ruler_status' ] ) ) {
?>
						<div class="blok">
							<strong><?php transprint( 'Status' ); ?>:</strong> <?php print( $_SESSION[ 'create_ruler_status' ] ); ?>
						</div>
						<br />
<?php
		unset( $_SESSION[ 'create_ruler_status' ] );
	}
	
	include ( "utils.php" );
	global $check_user_id;
	$game_turn = getCurrentTurn();
	
	$query = "SELECT count(*) FROM village WHERE start_place = 1 AND ruler_id = -1";
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
	$free = $row[ 'count(*)' ];
	
	$query = "SELECT * FROM map_settings";
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
	$starting_places = $row[ 'starting_places' ];
	$sector = $row[ 'sector' ];
	$sectors_per_row = $row[ 'sectors_per_row' ];
	$villages_per_ruler = $row[ 'villages_per_ruler' ];
	
	if( $free <= 0 ) { // perhaps set option somewhere, for expanding the world or not
		$sector ++;
		$check_user_id = -1;
		$bonus = 0;
		if( $game_turn < 2000 ) {
			$bonus = round( $game_turn / 40 );
		}
		else {
			$bonus = 50;
		}
		
		$x_start = $row[ 'x' ];
		$y_start = $row[ 'y' ];
		
		$query3 = "UPDATE map_settings SET updating = 1";
		mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
		
		$done = false;
		
		$query4 = "SELECT * FROM village_types WHERE start_place = 1";
		$result4 = mysql_query( $query4 ) or die( "Query failed : " . mysql_error() );
		$row4 = mysql_fetch_array( $result4, MYSQL_ASSOC );
		
		$query2 = "SELECT * FROM village_types WHERE start_place = 0";
		$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );	
		$num = mysql_num_rows( $result2 );
		
		while ( !$done ) {
			if( ( $sector - 1 ) % $sectors_per_row == 0 && $sector > 1 ) {
				$x_start = 0; 
				$y_start = $y_start + $row[ 'height' ];
			}
			else {
				$x_start = $x_start + $row[ 'width' ]; 
				$y_start = $y_start;
			}
			
			//print( "<br />this sector starts at ($x_start, $y_start) <br />" );
			
			$count = 0;
			$total = 0;
			$not_full = true;
			$x = $x_start;
			$y = $y_start;
			
			if( isEmptySector( $sector, $sectors_per_row ) && $row[ 'width' ] > 32 && $row[ 'height' ] > 32 ) {
				//print( "<br /> sector $sector is a free sector mother fucker!<br />" );
				// Create a sector without starting locations and only a few villages in the center
				$empty_width = $row[ 'width' ] - 32;
				$empty_height = $row[ 'height' ] - 32;
				$border = 16;
				
				$x += $border;
				$y += $border;
				$first = true;
				
				while ( $not_full ) {
					$x += 1 + ( ( int ) rand( $row[ 'min_distance' ], $row[ 'max_distance' ] ) );
					while ( $x >= $x_start + $border + $empty_width ) {
						$x -= $empty_width;
						if( !$first ) {
							$y ++;
						}
					}
					if( $y >= $y_start + $border + $empty_height ) {
						$not_full = false;
					}
					if( $not_full ) {
						$chance = rand( 0, 100 );
						$not_found = true;
						$i = 0;
						$chance_gap = 0;
						while ( $i < $num && $not_found ) {
							$chance_gap += mysql_result( $result2, $i, 'chance' ) * 100;
							if( $chance < $chance_gap || $chance_gap == 100 ) {
								$not_found = false;
								$coal = rand( mysql_result( $result2, $i, 'min_coal' ), mysql_result( $result2, $i, 'max_coal' ) );
								$iron = rand( mysql_result( $result2, $i, 'min_iron' ), mysql_result( $result2, $i, 'max_iron' ) );
								$food = rand( mysql_result( $result2, $i, 'min_food' ), mysql_result( $result2, $i, 'max_food' ) );
								$space = rand( mysql_result( $result2, $i, 'min_space' ), mysql_result( $result2, $i, 'max_space' ) );
								$start_place = 0;
								$population = 4000;
								$type = mysql_result( $result2, $i, 'village_type_id' );
							}
							$i ++;
						}
						$villageMapDirection = rand( 0, 3 );
						$query3 = "INSERT INTO village ( population, x, y, food_quality, iron_quality, coal_quality, start_place, space, village_type_id, add_turn, village_map_direction ) " .
												"VALUES ( $population, $x, $y, $food, $iron, $coal, $start_place, $space, $type, $game_turn, $villageMapDirection )";
						$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );

						$query3 = "SELECT village_id FROM village WHERE x = $x AND y = $y";
						//print( "get village id: $query3 <br />" );
						$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
						$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );
						$village_id = $row3[ 'village_id' ];
		
						$query3 = "INSERT INTO village_buildings ( village_id, building_id, ammount ) " .
								"VALUES ( "	. $village_id . ", 0, 1 )";
						//print( "put in building: $query3 <br />" );
						$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
					}
					$first = false;
				}
			}
			else {
				while ( $not_full ) {
					$x += 1 + ( ( int ) rand( $row[ 'min_distance' ], $row[ 'max_distance' ] ) );
					while( $x >= $x_start + $row[ 'width' ] ) {
						$x -= $row[ 'width' ];
						$y ++;
						if( $y >= $y_start + $row[ 'height' ] ) {
							$not_full = false;
							$done = true;
						}
					}
					if( $not_full ) {
						if( $total % $villages_per_ruler == 1 ) {
							$coal = rand( $row4[ 'min_coal' ], $row4[ 'max_coal' ] );
							$iron = rand( $row4[ 'min_iron' ], $row4[ 'max_iron' ] );
							$food = rand( $row4[ 'min_food' ], $row4[ 'max_food' ] );
							$space = rand( $row4[ 'min_space' ], $row4[ 'max_space' ] ) + $bonus;
							$start_place = 1;
							$population = 10000;
							$type = $row4[ 'village_type_id' ];
							$count ++;
						}
						else {
							$chance = rand( 0, 100 );
							$not_found = true;
							$i = 0;
							$chance_gap = 0;
							while ( $i < $num && $not_found ) {
								$chance_gap += mysql_result( $result2, $i, 'chance' ) * 100;
								if( $chance < $chance_gap || $chance_gap == 100 ) {
									$not_found = false;
									$coal = rand( mysql_result( $result2, $i, 'min_coal' ), mysql_result( $result2, $i, 'max_coal' ) );
									$iron = rand( mysql_result( $result2, $i, 'min_iron' ), mysql_result( $result2, $i, 'max_iron' ) );
									$food = rand( mysql_result( $result2, $i, 'min_food' ), mysql_result( $result2, $i, 'max_food' ) );
									$space = rand( mysql_result( $result2, $i, 'min_space' ), mysql_result( $result2, $i, 'max_space' ) );
									$start_place = 0;
									$population = 4000;
									$type = mysql_result( $result2, $i, 'village_type_id' );
								}
								$i ++;
							}
						}
						$villageMapDirection = rand( 0, 3 );
						$query3 = "INSERT INTO village ( population, x, y, food_quality, iron_quality, coal_quality, start_place, space, village_type_id, add_turn, village_map_direction ) " .
												"VALUES ( $population, $x, $y, $food, $iron, $coal, $start_place, $space, $type, $game_turn, $villageMapDirection )";
						$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
						
						$query3 = "SELECT village_id FROM village WHERE x = $x AND y = $y";
						$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
						$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );
						$village_id = $row3[ 'village_id' ];
		
						$query3 = "INSERT INTO village_buildings ( village_id, building_id, ammount ) " .
								"VALUES ( "	. $village_id . ", 0, 1 )";
						$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
						
						if( $start_place == 1 ) {
							$query3 = "INSERT INTO village_buildings ( village_id, building_id, ammount ) " .
									"VALUES ( "	. $village_id . ", 1, 2 )";
							$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
		
							$query3 = "INSERT INTO village_buildings ( village_id, building_id, ammount ) " .
									"VALUES ( "	. $village_id . ", 5, 1 )";
							$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
		
							$query3 = "INSERT INTO village_buildings ( village_id, building_id, ammount ) " .
									"VALUES ( "	. $village_id . ", 26, 2 )";
							$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
						}
						else {
							$query3 = "INSERT INTO village_buildings ( village_id, building_id, ammount ) " .
									"VALUES ( "	. $village_id . ", 26, 1 )";
							$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
						}
						
						$check_user_id = -1;
						$total ++;
					}
				}
			}
			if( !$done ) {
				$sector ++;
			}
		}
		
		$query3 = "UPDATE map_settings SET sector = " . $sector . ", " .
				"x = $x_start, y = $y_start, starting_places = $count, updating = 0";
		$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
		$check_user_id = -1;
		$starting_places = $free = $count;
		
		$query = "SELECT * FROM map_settings";
		$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		$row = mysql_fetch_array($result, MYSQL_ASSOC);
	}
	
	if( isset( $_GET[ 'rulername' ] ) && formatInput( $_GET[ 'rulername' ] ) != '' ) {
		$username = formatInput( $_GET[ 'rulername' ] );
	}
	else {
		$query3 = "SELECT username FROM users WHERE user_id = " . $_SESSION[ 'user_id' ];
		$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
		$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );
		$username = $row3[ 'username' ];
	}
	
	if( isset( $_GET[ 'capital' ] ) && formatInput( $_GET[ 'capital' ] ) != '' ) {
		$village_name = formatInput( $_GET[ 'capital' ] );
	}
	else {
		$query3 = "SELECT name FROM village_names WHERE used = 0 ORDER BY RAND() LIMIT 1";
		$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
		$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );
		if( isset( $row3[ 'name' ] ) ) {
			$village_name = $row3[ 'name' ];
		}
		else { 
			$village_name = "";
		}
	}
	
	$query3 = "SELECT civilian_news, operations_news, military_news, letters, news_letter, inactivity_notification " .
			"FROM users " .
			"WHERE user_id = " . $_SESSION[ 'user_id' ];
	$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
	$userNotifications = mysql_fetch_array( $result3, MYSQL_ASSOC );

	if( isset( $_SESSION[ 'user_just_registered' ] ) && $_SESSION[ 'user_just_registered' ] == 1 ) {
?>
					<div class="blok">
						<strong><?php transprint( 'Status' ); ?></strong>: <?php transprint( 'Your account has been activated' ); ?>.<br />
						<br />
						<?php transprint( 'Choose your name and capital name for this round.' ); ?><br />
						<?php transprint( 'Note: Your username and a random city name are already filled in, but feel free to change them.' ); ?><br />
					</div>
					<br />
<?php
	}
?>
					<div class="blok">
<?php
	print( translate( 'Current sector' ) . ": " . $sector . "<br />\r\n" );
	print( translate( 'Sector range: x' ) . ": " . $row[ 'x' ] . "-" . ( $row[ 'x' ] + $row[ 'width' ] ) . ", y: " . $row[ 'y' ] . "-" . ( $row[ 'y' ] + $row[ 'height' ] ) . "<br />\r\n" );
	print( translate( 'Capitals available' ) . ": " . $free . " " . translate( 'out of' ) . " " . number_format( $starting_places ) . " " . translate( 'capitals in this sector' ) . "<br />\r\n" );
?>
					</div>
					<br />
					<form action="create_ruler.php" method="post" id="form">
						<table>
							<tr>
								<td><?php transprint( 'Ruler name' ); ?>:</td>
								<td><input type="text" name="rulername" size="50" value="<?php print( $username ); ?>"/></td>
							</tr>
							<tr>
								<td><?php transprint( 'Capital name' ); ?>:</td>
								<td><input type="text" name="cityname" size="50" value="<?php print( $village_name ); ?>" /></td>
							</tr>
						</table>
						<br />
						<table>
							<tr><td colspan="3"><strong><?php transprint( 'Email notifications on' ); ?></strong></td></tr>
							<tr>
								<td><input class="notification-checkbox" type="checkbox" name="inactivity" id="notification-inactivity"<?php print( $userNotifications[ 'inactivity_notification' ] == 1 ? ' checked="checked"' : '' ); ?> /></td>
								<td colspan="2"><label for="notification-inactivity"><?php transprint( 'time to take action' ); ?></label> (<?php transprint( 'recommended' ); ?>)</td>
							</tr>
							<tr>
								<td><input class="notification-checkbox" type="checkbox" name="letters" id="notification-letters"<?php print( $userNotifications[ 'letters' ] == 1 ? ' checked="checked"' : '' ); ?> /></td>
								<td colspan="2"><label for="notification-letters"><?php transprint( 'new ingame messages' ); ?></label></td>
							</tr>
							<tr>
								<td><input class="notification-checkbox" type="checkbox" name="civilian_news" id="notification-civilian-news"<?php print( $userNotifications[ 'civilian_news' ] == 1 ? ' checked="checked"' : '' ); ?> /></td>
								<td colspan="2"><label for="notification-civilian-news"><?php transprint( 'news about my capital' ); ?></label></td>
							</tr>
							<tr>
								<td><input class="notification-checkbox" type="checkbox" name="operations_news" id="notification-operations-news"<?php print( $userNotifications[ 'opreations_news' ] == 1 ? ' checked="checked"' : '' ); ?> /></td>
								<td colspan="2"><label for="notification-operations-news"><?php transprint( 'news about army movement and missions' ); ?></label></td>
							</tr>
							<tr>
								<td><input class="notification-checkbox" type="checkbox" name="military_news" id="notification-military-news"<?php print( $userNotifications[ 'military_news' ] == 1 ? ' checked="checked"' : '' ); ?> /></td>
								<td colspan="2"><label for="notification-military-news"><?php transprint( 'news about battles' ); ?></label></td>
							</tr>
							<tr>
								<td><input class="notification-checkbox" type="checkbox" name="newsletter" id="notification-newsletter"<?php print( $userNotifications[ 'news_letter' ] == 1 ? ' checked="checked"' : '' ); ?> /></td>
								<td colspan="2"><label for="notification-newsletter"><?php transprint( 'newsletter with changes and news about the game' ); ?></label></td>
							</tr>
							<tr>
								<td>&nbsp;</td>
								<td><a href="javascript:void(null);" onclick="AOS.utils.selectAllNotifications();"><?php transprint( 'Select all' ); ?></a></td>
								<td><input type="button" value="<?php transprint( 'Confirm' ); ?>" id="start" onclick="disableElement( 'start', 'form' )" /></td>
							</tr>
						</table>
					</form>
					<br />
<?php
include( 'game_rules.php' );
?>		
				</div>
			</div>
		</div>
		<script type="text/javascript" src="merge_javascript.php?version=<?php require('merge_javascript.php'); ?>"></script>
	</body>
</html>
<?php
}
?>
